package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author:hujunwei
 * @description:角色管理实现类
 * @date:2019/7/29
 * @version:1.0
 */
@Service
@Slf4j
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;


    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    /*
     * @Author:hujunwei
     * @功能:查询角色列表，用于页面回显
     * @date: 2019/7/29
     * @param currentPage //当前页
     * @param pageSize    //每页记录数
     * @param queryString //查询条件
     * @return com.itheima.health.entity.PageResult
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /*
     * @Author:hujunwei
     * @功能:新增角色，同时新增角色与权限的中间表
     * @date: 2019/7/29
     * @param role
     * @param permissionIds
     * @return com.itheima.health.entity.Result
     */
    @Override
    public Result add( Role role,Integer[] menuIds,Integer[] permissionIds) {
        try {

            //新增角色
            roleDao.add(role);
            //新增角色和权限的中间表
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> role_permission = new HashMap<>();
                role_permission.put("role_id",role.getId());
                role_permission.put("permission_id",permissionId);
                permissionService.addRoleAndPermission(role_permission);
            }
            //新增角色和菜单的中间表
            for (Integer menuId : menuIds) {
                Map<String, Integer> role_menu = new HashMap<>();
                role_menu.put("role_id",role.getId());
                role_menu.put("menu_id",menuId);
               menuDao.addRoleAndMenu(role_menu);
            }
            return new Result(true,"新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增角色失败");
        }
    }

    /*
     * @Author:hujunwei
     * @功能:基于id删除角色
     * @date: 2019/7/30
     * @param id
     * @return com.itheima.health.entity.Result
     */
    @Override
    public Result deleteById(Integer id) {
        try {
            //删除角色权限中间表
            roleDao.deleteRoleAndPermissionByRoleId(id);
            //删除角色菜单中间表
            menuDao.deleteByRoleId(id);
            //删除角色表
            roleDao.deleteById(id);

            return new Result(true,"删除角色成功");
            } catch (Exception e) {
                e.printStackTrace();
            return new Result(false,"删除角色失败");
            }
    }

    /*
     * @Author:hujunwei
     * @功能:编辑角色信息
     * @date: 2019/7/30
     * @param id
     * @return com.itheima.health.entity.Result
     */
    @Override
    public Result edit(Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            // 保存角色信息
            roleDao.edit(role);
            //删除角色与权限关联
            roleDao.deleteRoleAndPermissionByRoleId(role.getId());
            //删除角色与菜单关联
            roleDao.deleteRoleAndMenuByRoleId(role.getId());
            // 保存新的关系
            for (Integer permissionId : permissionIds) {
                Map maps1 = new HashMap<>();
                maps1.put("permission_id", permissionId);
                maps1.put("role_id", role.getId());
                permissionDao.addRoleAndPermission(maps1);
            }
            // 保存新的关系
            for (Integer menuId : menuIds) {
                Map maps2 = new HashMap<>();
                maps2.put("menu_id", menuId);
                maps2.put("role_id", role.getId());
                permissionDao.addRoleAndMenu(maps2);
            }
            
            return new Result(true,"修改角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改角色信息失败");
        }
    }

    /*
     * @Author:hujunwei
     * @功能:查询角色信息
     * @date: 2019/7/30
     * @param id
     * @return com.itheima.health.entity.Result
     */
    @Override
    public Result findById(Integer id) {
        try {
            Page<Role> roles = roleDao.selectByCondition(id.toString());
            Role role=null;
            for (Role role1 : roles) {
                role=role1;
            }
            return new Result(true,"查询角色信息成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色信息失败");
        }
    }
}
